package ares

import (
	"aquaman/app/biz"
	"aquaman/app/infra/config"
	"aquaman/app/infra/db"
	"aquaman/app/infra/job"
	"aquaman/app/infra/job/interceptors"
	"aquaman/app/infra/log"
	"context"
	"github.com/google/wire"
	"os"
	"os/signal"
	"syscall"
	//"github.com/robfig/cron"
	"time"
)



type Ares struct {
	logger 	log.Logger
	task	job.Task
	ticker 	*time.Ticker
	interceptors []job.TaskInterceptor
}


func (job *Ares) Run(ctx context.Context) error {
	job.logger.Info("Ares Job start")

	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

	handler := job.task
	for _, i := range job.interceptors {
		handler = i(handler)
	}
	handler(ctx)

	//cron := cron.New()
	//cron.AddFunc(job.spec, handler)

	for {
		select {
		case <-job.ticker.C:
			handler(ctx)
		case <-quit:
			job.logger.Info("Ares Job exit")
			return nil
		}
	}

}

func NewAres(
	l log.Logger,
	t job.Task,
	ticker *time.Ticker,
	is []job.TaskInterceptor,
) job.Job {
	return &Ares {
		logger: l,
		task:   t,
		ticker:  ticker,
		interceptors: is,
	}
}

var ProviderSet = wire.NewSet(
	NewAres,
	interceptors.ProviderSet,
	biz.SuperSet,
	db.ProviderSet,
	log.ProviderSet,
	config.ProviderSet,
)